# -*- coding: utf-8 -*
__author__ = 'dell'
from operator import itemgetter, attrgetter
import MySQLdb
import socket
import cal_function

def calAndWriteRecord(cur,ship_id,ship_type,max_speed,max_power,amr,efa_pm10,efa_pm2_5,efa_nox,efa_sox,efbi_pm10,efbi_pm2_5,efbi_nox,efbi_sox,
                      efi_pm10,efi_pm_2_5,efi_nox,efi_sox,starTime, endTime, lon, lat, ship_speed):   ########计算一个小时内的排放量，返回一条数据,记录在表emission_record_one_hour_real_time中

    emission=[0,0,0,0]

    time = endTime - starTime

    emission_temp = cal_function.calEmission(cur,time,ship_type,ship_speed,max_speed,max_power,amr,
                       efa_pm10,efa_pm2_5,efa_nox,efa_sox,efbi_pm10,efbi_pm2_5,efbi_nox,efbi_sox,efi_pm10,efi_pm_2_5,efi_nox,efi_sox)

    info = cur.fetchmany(cur.execute("insert into emission values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')"
                                     %(ship_id,ship_type,starTime,endTime,int(lon),int(lat),
                                       emission[0],emission[1],emission[2],emission[3],sum(emission))))






def emission_real_time(u,p,database_name):

    ##　ship dictionary
    ship_dict = {}

    ## connect database
    conn= MySQLdb.connect(host='localhost',port = 3306,user=u,passwd=p,db =database_name,)
    cur = conn.cursor()

    ## build tcp server
    host = ""
    port = 8888
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((host, port))
    s.listen(5)
    sock, addr = s.accept()

    ## recv message
    while 1:
        ais_message = sock.recv(1024)
        print  ais_message
        new_ship =  ais_message.split("~")[0]
        new_ship_time = int(ais_message.split("~")[1])

        if new_ship in ship_dict:
            starTime = ship_dict[new_ship]
            endTime = new_ship_time
            lon = int(ais_message.split("~")[3])
            lat = int(ais_message.split("~")[4])
            time = endTime - starTime
            ship_speed = int(ais_message.split("~")[7])

            # 将ship_id对应的船舶劳氏数据信息读如变量[]
            ship_id_info = cur.fetchmany(cur.execute("select * from ship_info where ship_id = %d" % ship_id))[0]
            [ship_id, ship_type, max_speed, max_power, year, machine_type, machine_speed] = list(ship_id_info)
            #########计算相关参数，除了与速度有关的参数
            amr = cur.fetchmany(cur.execute("select * from amr where ship_type = '%s'" % ship_type))[0][1]
            [year, efa_pm10, efa_pm2_5, efa_nox, efa_sox] = list(
                (cur.fetchmany(cur.execute("select * from efa where year = '%s'" % year)))[0])
            [efbi_pm10, efbi_pm2_5, efbi_nox, efbi_sox] = list(cur.fetchmany(cur.execute("select * from efbi"))[0])
            [machine_type, year, efi_pm10, efi_pm_2_5, efi_nox, efi_sox] = list(cur.fetchmany(
                cur.execute("select * from efi where machine_type='%s' and year='%s'" % (machine_type, year)))[0])
            if int(max_speed) == 0:
                max_speed = cur.fetchmany(cur.execute("select * from ship_speed where ship_type = '%s'" % ship_type))[0][3]

            calAndWriteRecord(cur, ship_id, ship_type, max_speed, max_power, amr, efa_pm10, efa_pm2_5, efa_nox, efa_sox,
                              efbi_pm10, efbi_pm2_5, efbi_nox, efbi_sox,
                              efi_pm10, efi_pm_2_5, efi_nox, efi_sox, starTime, endTime, lon, lat, ship_speed)

        else:
            ship_dict[new_ship] = new_ship_time


    cur.close()
    conn.commit()
    conn.close()

if __name__ == "__main__" :
    emission_real_time('root','123456','ship_emision')
